﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DatNuocViet.Models;
using System.Net.Mail;

namespace DatNuocViet.Controllers
{
    public class ContactController : Controller
    {
        //
        // GET: /Contact/

        public ActionResult Index()
        {
            GetWebResourceResult model = new GetWebResourceResult();
            GetWebResourceResult model1 = new GetWebResourceResult();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.GetWebResource("datnuocviet.contact").FirstOrDefault();
                model1 = db.GetWebResource("datnuocviet.map").FirstOrDefault();
            }
            ViewData["Contact"] = model;
            ViewData["Map"] = model1;
            ViewBag.Title = Ultility.Helper.Label("Menu_lienhe");
            return View();
        }

        [HttpPost]
        public ActionResult SendContact(FormCollection form)
        {
            string email = form["Email"];
            string fullname = form["FullName"];
            string subject = form["Subject"];
            string content = form["Content"];
            if(email == string.Empty || fullname == string.Empty || subject == string.Empty || content== string.Empty)
                return Content("<script type='text/javascript'>window.location.replace('~');</script>");
            string mailbody = "<p>Họ tên người gửi: " + fullname + "</p>";
            mailbody += "<p>Địa chỉ email: " + email + "</p>";
            mailbody += "<p>Nội dung: " + content + "</p>";
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient cl = new SmtpClient();
                mail.From = new MailAddress(email);
                mail.To.Add("pethasonbao@gmail.com");
                mail.IsBodyHtml = true;
                mail.Subject = subject;
                mail.Body = mailbody;
                cl.Host = "smtp.gmail.com";
                cl.Port = 587;
                cl.EnableSsl = true;
                cl.UseDefaultCredentials = false;
                cl.Credentials = new System.Net.NetworkCredential("dnv.mail01@gmail.com", "PhongCanh123");
                cl.Send(mail);
                string message = DatNuocViet.Ultility.Helper.Label("mailsuccess");
                return Content("<script type='text/javascript'>alert('" + message + "');window.location.replace('" + Url.Content("~/Contact") + "');</script>");
            }
            catch (Exception)
            {
                string message = DatNuocViet.Ultility.Helper.Label("mailfail");
                return Content("<script type='text/javascript'>alert('" + message + "');window.location.replace('" + Url.Content("~/Contact") + "');</script>");
            }
        }
    }
}
